package project.platform.mms;

import java.io.UnsupportedEncodingException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.SizeExceededException;

import project.Animal;
import project.platform.XML;

/**
 * 2BA7 Project MMS Communications Platform to interact with other phones using MMS messages.
 *
 * @author Paul Magrath - 05379725
 */
public class MMSPlatform {
	
	/** 
	 * Method to send an animal by MMS to another phone
	 * @param address Phone number in URL format e.g. mms://+5550001
	 * @param a The Animal to send to another phone
	 * @param sendingAlert an Alert to use for status
	 * @param errorAlert an Alert to use for errors
	 * @param appID This application's MMS application ID
	 * @param display Display that is being used
	 */
	public static void sendByMMS(final String address, final Animal a, final Alert sendingAlert,
			final Alert errorAlert, final String appID, final Display display){
		MMSMessage message;
		message = MMSPlatform.animalToMessage(a);
		MMSPlatform.promptAndSend(address, message, sendingAlert, errorAlert, display, appID);
	
	}

	/**
	 * Creates an MMS message for an Animal by encoding it in XML.
	 * @param a Animal to encode in XML.
	 * @return MMS message of the encoded Animal.
	 */
	private static MMSMessage animalToMessage(final Animal a){
		final MMSMessage message = new MMSMessage();
	    final String encoding = "UTF-8";
	    final String mimeType = "text/xml";
		byte[] contents;
		try {
			contents = XML.encodeToXML(a).getBytes(encoding);
			message.addPart(new MessagePart(contents, 0, contents.length, mimeType,
			        "id" + 0, "contentLocation", encoding));
		} catch (final SizeExceededException e) {
			e.printStackTrace();
		} catch (final UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return message;
	}

	/**
	 * Displays security warning and then sends the message.
	 * NB: Separate thread for sending needed to avoid 
	 * contention for display.
	 * 
	 * @param address Phone number to send to
	 * @param message Animal encoded in XML and in MMSMessage instance
	 * @param sendingAlert an Alert to use for status
	 * @param errorAlert an Alert to use for errors
	 * @param display Display that is being used
	 * @param appID This application's MMS application ID
	 */
    private static void promptAndSend(final String address, final MMSMessage message, final Alert sendingAlert,
    		final Alert errorAlert, final Display display, final String appID) {
        try {
            message.setDestination(address);
            final String statusMessage = "Sending message to " + address + "...";
            sendingAlert.setString(statusMessage);
            display.setCurrent(sendingAlert);
            new SenderThread(message, appID).start();
        } catch (final IllegalArgumentException iae) {
            errorAlert.setString(iae.getMessage());
            display.setCurrent(errorAlert);
        }
    }
	
	/** 
	 * Method to receive an animal by MMS from another phone.
	 * Starts a listener and then waits.
	 * 
	 * @param appID This application's MMS application ID
	 * @param display Display that is being used
	 * @param form Form that is being used
	 */
	public static void receiveMMS(final String appID, final Display display, final Form form){
		new ReceiverThread(appID, display, form);
	}
	
	/**
	 * Method to get animals received by MMS by thread started in receiveMMS()
	 * 
	 * @return The Animals received by MMS
	 */
	public static Animal[] getFromMMS(){
		return XML.decodeFromXML("<xml>" + ReceiverThread.getXML() + "</xml>");
	}
}
